package org.zizi.db.charset;

import java.io.UnsupportedEncodingException;

public class CharsetEncoder
{
	private String databaseCharset;		//数据库符集名称
	private String javaCharset;			//java程序符集
	
	public String encodeToJava(String source) throws UnsupportedEncodingException
	{
		if (source == null)
			return source;
		else if (databaseCharset == null && javaCharset == null)
			return source;
		else if (databaseCharset == null && javaCharset != null)
			return new String(source.getBytes(), javaCharset);
		else if (databaseCharset != null && javaCharset == null)
			return new String(source.getBytes(databaseCharset));
		else
			return new String(source.getBytes(databaseCharset), javaCharset);
	}
	
	public String encodeToDatabase(String source) throws UnsupportedEncodingException
	{
		if (source == null)
			return source;
		else if (databaseCharset == null && javaCharset == null)
			return source;
		else if (databaseCharset == null && javaCharset != null)
			return new String(source.getBytes(javaCharset));
		else if (databaseCharset != null && javaCharset == null)
			return new String(source.getBytes(), databaseCharset);
		else
			return new String(source.getBytes(javaCharset), databaseCharset);
	}
	
	public String getDatabaseCharset()
	{
		return databaseCharset;
	}
	public void setDatabaseCharset(String databaseCharset)
	{
		this.databaseCharset = databaseCharset;
	}
	public String getJavaCharset()
	{
		return javaCharset;
	}
	public void setJavaCharset(String javaCharset)
	{
		this.javaCharset = javaCharset;
	}
}
